package search.binarysearch;

/**
 * https://leetcode-cn.com/problems/xuan-zhuan-shu-zu-de-zui-xiao-shu-zi-lcof/
 * 查询最小值
 *
 */
public class FindMin {


    /**
     * 旋转有序集合中，查询最小值
     *
     * @param nums 集合
     * @return 最小值
     */
    public int findMin(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int start = 0;
        int end = nums.length - 1;
        int mid;
        while (start + 1 < end) {

            mid = start + (end - start) / 2;
            if (nums[mid] >= nums[start]) {
                if (nums[end] <= nums[mid]) {
                    start = mid;
                } else {
                    end = mid;
                }
            } else {
                //end = mid;
                start = mid;
            }
        }
        return Math.min(nums[start], nums[end]);
    }

    public static void main(String[] args) {
        //int[] array = {6, 5, 4, 3, 2, 1, 0};

        int[] array = {0, 1, 2, 3, 4, 5, 6};

        System.out.println(new FindMin().findMin(array));
    }
}